{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "collectible-alaska",
   "metadata": {},
   "source": [
    "# French Tax Identification Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "prepared-canberra",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "touched-infrastructure",
   "metadata": {},
   "source": [
    "The function `clean_fr_nif()` cleans a column containing French tax identification number (NIF) strings, and standardizes them in a given format. The function `validate_fr_nif()` validates either a single NIF strings, a column of NIF strings or a DataFrame of NIF strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "intimate-wagon",
   "metadata": {},
   "source": [
    "NIF strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"0701987765432\"\n",
    "* `standard`: NIF strings with proper whitespace in the proper places, like \"07 01 987 765 432\"\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_fr_nif()` and `validate_fr_nif()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "acute-florence",
   "metadata": {},
   "source": [
    "### An example dataset containing NIF strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "metric-breath",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"nif\": [\n",
    "            \"0701987765432\",\n",
    "            \"070198776543\",\n",
    "            \"51824753556\",\n",
    "            \"51 824 753 556\",\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\"\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dense-yesterday",
   "metadata": {},
   "source": [
    "## 1. Default `clean_fr_nif`\n",
    "\n",
    "By default, `clean_fr_nif` will clean nif strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cooperative-worship",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_fr_nif\n",
    "clean_fr_nif(df, column = \"nif\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "numeric-tunisia",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "lyric-coffee",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "accredited-stage",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "recognized-selection",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fr_nif(df, column = \"nif\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "administrative-nicholas",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "regular-handling",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fr_nif(df, column = \"nif\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "tested-straight",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned NIF strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "anonymous-canberra",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fr_nif(df, column=\"nif\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "crude-iraqi",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aggregate-entity",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "finished-butter",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fr_nif(df, \"nif\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "latter-disabled",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "developmental-cooler",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fr_nif(df, \"nif\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abroad-pregnancy",
   "metadata": {},
   "source": [
    "## 4. `validate_fr_nif()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "everyday-century",
   "metadata": {},
   "source": [
    "`validate_fr_nif()` returns `True` when the input is a valid NIF. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_fr_nif()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_fr_nif()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_fr_nif()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "faced-medium",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_fr_nif\n",
    "print(validate_fr_nif(\"0701987765432\"))\n",
    "print(validate_fr_nif(\"070198776543\"))\n",
    "print(validate_fr_nif(\"51824753556\"))\n",
    "print(validate_fr_nif(\"51 824 753 556\"))\n",
    "print(validate_fr_nif(\"hello\"))\n",
    "print(validate_fr_nif(np.nan))\n",
    "print(validate_fr_nif(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "religious-emergency",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "adequate-personal",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_fr_nif(df[\"nif\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "differential-parker",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "neural-student",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_fr_nif(df, column=\"nif\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "scenic-passport",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "boring-discrimination",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "validate_fr_nif(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "starting-church",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
